package com.zhongketech.sunflower.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.SpecialNums;

public class SpecialNumsManager {


	public static void save(List<SpecialNums> specialNumsList)
	{
		List<SpecialNums> list = specialNumsList;
		if(null == list)
		{
			list = new Action().specialnums();
		}

		LogUtil.i("ZK"," SpecialNumsManager save list:"+list);
		if(null != list && list.size() > 0)
		{
			Map<String,String> familyMap = new HashMap<String, String>();
			Map<String,String> emergencyMap = new HashMap<String, String>();
			Map<String,String> blackMap = new HashMap<String, String>();
			for(int i = 0;i < list.size();i ++)
			{
				if(Constant.SN_WHITLE.equals(list.get(i).getType()))
				{
					LogUtil.i("ZK"," save specialnums "+list.get(i).getGroup()+" "+list.get(i).getName()+" "+list.get(i).getPhone());
					if(Constant.SN_FAMILY.equals(list.get(i).getGroup()))
					{
						familyMap.put(list.get(i).getName(), list.get(i).getPhone());	
					}
					else if(Constant.SN_EMERGENCY.equals(list.get(i).getGroup()))
					{
						emergencyMap.put(list.get(i).getName(), list.get(i).getPhone());	
					}

				}
				else if(Constant.SN_BLACK.equals(list.get(i).getType()))
				{
					LogUtil.i("ZK"," save specialnums SN_BLACK "+list.get(i).getGroup()+" "+list.get(i).getName()+" "+list.get(i).getPhone());
					blackMap.put(list.get(i).getName(), list.get(i).getPhone());	
				}
			}
			if(familyMap.size() > 0)
			{
				MyApplication.getInstance().setSpecialNumberList(familyMap, Constant.SN_FAMILY);
			}
			if(emergencyMap.size() > 0)
			{
				MyApplication.getInstance().setSpecialNumberList(emergencyMap, Constant.SN_EMERGENCY);
			}
			if(blackMap.size() > 0)
			{
				MyApplication.getInstance().setSpecialNumberList(blackMap, Constant.SN_BLACK);
			}
		}
		else
		{
			MyToast.getInstance().show("没有特殊号码");
		}
	}

	public static List<SpecialNums> getSpecialNums(String type)
	{
		List<SpecialNums> list = null;
		LogUtil.i("ZK","getSpecialNums: "+type);
		Map<String, String> map = MyApplication.getInstance().getSpecialNumberList(type);
		if(null != map && map.size() > 0)
		{
			list = new ArrayList<SpecialNums>();
			final Set set = map.entrySet();
			final Iterator it = set.iterator();
			while (it.hasNext()) { 
				Map.Entry mapEntry = (Map.Entry) it.next();
				SpecialNums sn = new SpecialNums();

				LogUtil.i("ZK","getSpecialNums: "+mapEntry.getKey()+" "+ mapEntry.getValue());

				if(Constant.SN_BLACK.equals(type))
				{
					sn.setGroup(Constant.SN_BLACK);
				}
				else
				{
					sn.setGroup(Constant.SN_WHITLE);
					sn.setType(type);
				}
				sn.setName((String) mapEntry.getKey());
				sn.setPhone((String) mapEntry.getValue());
				list.add(sn);
			}
		}

		return list;
	}
}
